//导入express
const express = require("express");
//实例化路由对象
const apiRouter = express.Router();

//引入addUser.js
const addUser = require("../service/addUser");
//引入users.js
const users = require("../model/users");
//引入userLogin.js
const userLogin = require("../service/userLogin");
//引入articleList.js
const articleList = require("../service/articleList");
//引入usersList.js
const usersList = require("../service/usersList");
//引入classifyArticleList.js
const classifyArticleList = require("../service/classifyArticleList");
//引入findArticleDetails.js
const findArticleDetails = require("../service/findArticleDetails");
//引入myArticleList 
const myArticleList = require("../service/myArticleList");
//引入article.js
const article = require("../model/article");
//引入addArticle.js
const addArticle=require("../service/addArticle");
//引入deleteArticle.js
const deleteArticle=require("../service/addArticle");
const { ObjectId } = require("mongodb");
//引入updateIdArticle.js
const updateIdArticle=require("../service/updateIdArticle");

//获取首页文章数据
apiRouter.get("/articleList",(req,res,next)=>{
    //调用获取文章列表
    articleList(res);

})
//获取首页推荐作者
apiRouter.get("/authorsList",(req,res,next)=>{
    //调用查询所有用户
    usersList(res);

})

//文章分类页加载文章列表数据
apiRouter.get("/classifyArticleList",(req,res,next)=>{
    //接收参数
    let classify = req.query.classify;
    //调用根据分类加载文章列表
    classifyArticleList(classify,res);
})

//获取文章详情数据
apiRouter.get("/findArticleDetails",(req,res,next)=>{
    //接收参数
    let id = req.query.id;
    //调用根据id查询文章的方法
    findArticleDetails(id,res);
})

//用户登录
apiRouter.post("/userLogin",(req,res,next)=>{
    //接收参数
    let body = req.body;
    let userName = body.userName;
    let userPassword = body.userPassword;

    //参数转json
    users.userName = userName;
    users.userPassword = userPassword;

    //调用登录的方法
    userLogin(users,res);


})
//注册路由请求
apiRouter.post("/addUser",(req,res,next)=>{
    //接收传进来的参数
    let body = req.body;
    let userName = body.userName;
    let userPassword = body.userPassword;
    let desc = body.desc;
    
    users.userId=new Date().getTime();
    users.userName = userName;
    users.userPassword = userPassword;
    users.desc = desc;
    users.createTime = new Date();
    //调用service添加的方法
    addUser(users,res);
    
   
})
//加载个人文章列表，参数为个人id
apiRouter.get("/myArticleList",(req,res,next)=>{
    //接收参数
    let userId = req.query.userId;
    //调用根据userId查找文章的方法
    myArticleList(userId,res);
})

//新增文章
apiRouter.post("/addArticle",(req,res,next)=>{
    //接收参数
    let body = req.body;
    let title = body.title;
    let content = body.content;
    let desc = body.desc;
    let author = body.author;
    let authorId = body.authorId;
    let classify = body.classify;
    //参数转化为json数据格式

    article.author=author;
    article.authorId=authorId;
    article.classify=classify;
    article.content=content;
    article.desc=desc;
    article.title=title;
    //调用添加文章方法
    addArticle(article,res);

})
//更新文章
apiRouter.post("/updateIdArticle",(req,res,next)=>{
    //接收参数
    let body = req.body;
    let title = body.title;
    let content = body.content;
    let desc = body.desc;
    let author = body.author;
    let authorId = body.authorId;
    let classify = body.classify;
    let id=body.id;

    article.author=author;
    article.authorId=authorId;
    article.classify=classify;
    article.content=content;
    article.desc=desc;
    article.title=title; 
    //调用法更新方法
    updateIdArticle(new ObjectId(id),article,res);


})
//删除文章
apiRouter.get("/deleteArticle",(req,res,next)=>{
    //接受参数
    let id = req.query.id;
    //调用删除方法
    deleteArticle(new ObjectId(id),res);
})
//导出
module.exports = apiRouter;

